<template>
  <el-aside :width="isCollapse ? '56px' : '200px'">
<!--    折叠按钮-->
    <div class="toggle-button" @click="toggleCollapse">
      |||
    </div>
<!--    collapse控制折叠-->
<!--    transition控制折叠动画-->
<!--    只打开一个菜单-->
<!--    将index设置为路由-->
<!--    设置默认高亮路由-->
    <el-menu
        :collapse="isCollapse"
        :collapse-transition="false"
        :uniqueOpened="true"
        :router="true"
        :default-active="activePath"
        class="el-menu-vertical-demo"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#409eff">
<!--index是标识符 一样会导致所有的menu动作相同-->
<!--      index只接受字符串-->
<!--      一级菜单-->
      <el-menu-item :index="'/welcome'">
        <template #title>
          <i class="el-icon-s-custom"></i>
          <span>首页</span>
        </template>
      </el-menu-item>
      <!--      一级菜单-->
      <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
        <template #title>
          <i class="el-icon-s-custom"></i>
          <span>{{item.authName}}</span>
        </template>
<!--      二级菜单-->
          <el-menu-item
              :index="'/'+childItem.path"
              v-for="childItem in item.children" :key="childItem.id"
              @click="getPath(childItem.path)">
            <template #title>
              <i class="el-icon-s-operation"></i>
              <span>{{childItem.authName}}</span>
            </template>
          </el-menu-item>

      </el-submenu>
    </el-menu>
  </el-aside>
</template>

<script>
export default {
  name: "SideMenu",
  data(){
    return{
      isCollapse: false,
      activePath:'',
    }
  },
  props:['menuList'],
  created() {
    this.activePath = '/' + window.sessionStorage.getItem('path')
    },
  methods:{
    //左边导航栏的折叠与展开
    toggleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    getPath(path){
      //将点击的path存储在sessionStorage里面
      window.sessionStorage.setItem('path',path)
      //console.log(window.sessionStorage.getItem('path'))
      this.activePath = '/' + window.sessionStorage.getItem('path')
      //console.log(this.activePath)
    },
  }
}
</script>

<style lang="less" scoped>
.el-menu-vertical-demo{
  height: 100vh;
  .el-menu-item{
    border-right: 0;
  }
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  //文本居中
  text-align: center;
  //字符间隔
  letter-spacing: 0.3em;
  //鼠标变化
  cursor: pointer;
}

</style>